package com.marver.common.sms;

import blade.kit.http.HttpRequest;
import blade.kit.json.JSONKit;
import com.alibaba.fastjson.JSONObject;
import com.marver.common.result.Result;
import com.marver.common.result.ResultStatus;
import com.marver.common.utils.PropUtil;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class SmsChuangLan implements Sms {

    private String signature=PropUtil.getProperty("sms.chuanglan.signature");
    private String account=PropUtil.getProperty("sms.chuanglan.account");
    private String pswd=PropUtil.getProperty("sms.chuanglan.pswd");
    private String serverUrl=PropUtil.getProperty("sms.chuanglan.serverUrl");
    private String msgUrl=PropUtil.getProperty("sms.chuanglan.msgUrl");
    private String balanceUrl=PropUtil.getProperty("sms.chuanglan.balanceUrl");
    private String delayUrl=PropUtil.getProperty("sms.chuanglan.delayUrl");
    private String variableUrl=PropUtil.getProperty("sms.chuanglan.variableUrl");

    /**
     * @MethodName:sendMessage
     * @Param:
     * @Author:wujc
     * @Date:2017-03-22
     * @Return:
     * @Descb: 发送普通短信方法
     * @Throws:
     */
    public Result sendMessage(String phone, String msg){
        return sendMessage(phone, msg, "1", null);
    }

    private Result sendMessage(String phone, String msg, String rd, String extno) {
        String postUrl = serverUrl + msgUrl;
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("account", account);
            params.put("password", pswd);
            params.put("phone", phone);//短信接收号码,多个号码用英文,隔开
            params.put("msg", msg);//您的签名+短信内容
            params.put("rd", rd);// 是否需要状态报告(需要:1,不需要:0)
            params.put("extno", extno);// 扩展码(可选参数,可自定义)

            String response = HttpRequest
                .post(postUrl)
                .trustAllHosts()
                .trustAllCerts()
                .contentType("application/json")
                .send(JSONKit.toJSONString(params))
                .body();
        JSONObject result=JSONObject.parseObject(response);
        if(result.getString("code").equals("0")){
            return new Result(ResultStatus.SUCCESS.getErrorCode());
        }
        return new Result(ResultStatus.FAIL.getErrorCodeToString(),result.getString("errorMsg"));
    }

    /**
     * @MethodName:getbalance
     * @Param:
     * @Author:wujc
     * @Date:2017-03-22
     * @Return:
     * @Descb: 查询账户余额
     * @Throws:
     */
    public void getbalance() {
        String postUrl = serverUrl + balanceUrl;
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("un", account);
            params.put("pw", pswd);

            String response = HttpRequest
                    .post(postUrl)
                    .trustAllHosts()
                    .trustAllCerts()
                    .form(params)
                    .body();

            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
